'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _select = require('antd/lib/select');

var _select2 = _interopRequireDefault(_select);

var _AutoComplete = require('./AutoComplete');

var _AutoComplete2 = _interopRequireDefault(_AutoComplete);

var _Color = require('./Color');

var _Color2 = _interopRequireDefault(_Color);

var _InputGroup = require('./InputGroup');

var _InputGroup2 = _interopRequireDefault(_InputGroup);

var _utils = require('../../utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var Option = _select2['default'].Option;

var BoxModel = function (_Component) {
  (0, _inherits3['default'])(BoxModel, _Component);

  function BoxModel(props) {
    (0, _classCallCheck3['default'])(this, BoxModel);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (BoxModel.__proto__ || Object.getPrototypeOf(BoxModel)).call(this, props));

    _this.getInput = function (isRadius) {
      var value = _this.props.value;

      return (isRadius ? _this.radioKeys : _this.props.keys).map(function (key) {
        var v = value && value[key] || (typeof value === 'string' ? value : null);
        if (key === 'center') {
          var values = Object.keys(value).map(function (c) {
            return value[c];
          });
          var equal = values.every(function (c) {
            return c === values[0];
          });
          v = equal ? values[0] : v;
        }
        return _react2['default'].createElement(_AutoComplete2['default'], {
          key: key,
          value: v,
          className: key,
          placeholder: key,
          disabled: _this.props.disabled
        });
      });
    };

    _this.getColor = function () {
      var value = _this.props.value;

      return _this.props.keys.map(function (key) {
        var v = value && value[key] || (typeof value === 'string' ? value : null);
        return _react2['default'].createElement(_Color2['default'], {
          key: key,
          type: 'cut',
          className: key,
          color: v
        });
      });
    };

    _this.getSelect = function () {
      var select = ['none', 'solid', 'dashed', 'dotted', 'double'];
      var value = _this.props.value;

      return _this.props.keys.map(function (key) {
        var v = value && value[key] || (typeof value === 'string' ? value : select[0]);
        return _react2['default'].createElement(
          _select2['default'],
          {
            className: key,
            size: 'small',
            key: key,
            value: v,
            getPopupContainer: function getPopupContainer(node) {
              return (0, _utils.getParentNode)(node, 'editor-list');
            },
            dropdownMatchSelectWidth: false,
            dropdownClassName: 'editor-list-dropdown'
          },
          select.map(function (cKey) {
            return _react2['default'].createElement(
              Option,
              { value: cKey, key: cKey },
              cKey
            );
          })
        );
      });
    };

    _this.getChildrenToBox = function () {
      switch (_this.props.name) {
        case 'color':
          return _this.getColor();
        case 'style':
          return _this.getSelect();
        default:
          return _this.getInput(_this.props.name === 'radius');
      }
    };

    _this.radioKeys = ['top-left', 'top-right', 'bottom-right', 'bottom-left', 'center'];
    return _this;
  }

  (0, _createClass3['default'])(BoxModel, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      return _react2['default'].createElement(
        'div',
        { className: 'box-model-wrapper' },
        _react2['default'].createElement(
          _InputGroup2['default'],
          { className: 'box-model', onChange: function onChange(v, isDrag) {
              _this2.props.onChange(v, isDrag);
            } },
          this.getChildrenToBox()
        )
      );
    }
  }]);
  return BoxModel;
}(_react.Component);

BoxModel.propTypes = {
  value: _propTypes2['default'].any,
  onChange: _propTypes2['default'].func,
  keys: _propTypes2['default'].array,
  name: _propTypes2['default'].string,
  disabled: _propTypes2['default'].bool
};
BoxModel.defaultProps = {
  keys: ['top', 'right', 'bottom', 'left', 'center'],
  onChange: function onChange() {}
};
exports['default'] = BoxModel;
module.exports = exports['default'];